package until;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.bean.Admin;
import com.bean.Users;


public class LoginInterceptor implements HandlerInterceptor{

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		
		//获取请求的URL  
        String url = request.getRequestURI();
        System.out.println(url+"----------->");
       
        //URL:login.jsp是公开的;这个demo是除了login.jsp是可以公开访问的，其它的URL都进行拦截控制  
        if(url.indexOf("userLogin.do")>=0 ||url.indexOf("regUser.do")>=0 || url.indexOf("AdminLogin.do")>=0 || url.indexOf("findFilmByStatus1.do")>=0
        	||url.indexOf("toShowAllCiname.do")>=0 ||url.indexOf("tofindFilmsByLike1.do")>=0 || url.indexOf("tofindCinameByLike1.do")>=0	){ 
            return true;  
        } 
       
        //获取Session  
        HttpSession session = request.getSession();  
        Users user = (Users) session.getAttribute("user");
        Admin admin = (Admin) session.getAttribute("admin");
          
        if(user != null || admin!=null ){  
            return true;  
        }
        
        System.out.println(request+"____________>");
        //不符合条件的，跳转到登录界面  
        request.getRequestDispatcher("/index.jsp").forward(request, response);  
		return false;
	}
	


}
